"""
A class to exercise the JT core functions.
"""
import JT2 as JT
import Calibration
import unittest
from IPython.Shell import IPShellEmbed


class Test(unittest.TestCase):
    def runTest(self):
        """
        Test out the ITK get metric procedure.
        """

#===============================================================================
#
#    Data Paths
#
#===============================================================================

        fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
        movingImageName = 'C:/Users/bryan/bryan-code/trunk/Images/CalibratedDRRImage.mhd'
        inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
        staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
        calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'


#===============================================================================
#
#    Setup DRR
#
#===============================================================================

        xraycam = JT.XRayCamera()
        drr = JT.DRR()
        drr.SetXRayCamera(xraycam)
        drr.SetBlackOnWhiteImage()
        drr.InteractionOff()

        cal = Calibration.ExternalCalibration()
        cal.LoadConsolidatedCalibration(calFileLat)
        cal.LoadStaFile(staFile)

        xraycam.SetExternalCalibration(cal)

        volume = JT.Volume()
        volume.SetVolumeFilename(inputVolFileName)
        volume.SetOriginalTransform(cal._VolumeT)
        volume.SetBodyCenter((3.906, 2.17, -7.378))
        drr.AddVolume(volume)

#===============================================================================
#
#    NULL 2D metric (should be 0)
#
#===============================================================================
#        print "Starting Registration 1..."
#        reg1=JT.Registration()
#        reg1.SetFixedFileName(fixedImageName)
#        reg1.SetMovingFileName(fixedImageName)
#        val1 = reg1.GetMetric()
        val1=0

#===============================================================================
#
#    Real-life 2D metric (should not be 0)
#
#===============================================================================
#        print "Starting Registration 2..."
#        reg2=JT.Registration()
#        reg2.SetFixedFileName(fixedImageName)
#        reg2.SetMovingFileName(movingImageName)
#        val2 = reg2.GetMetric()
        val2=1

#===============================================================================
#
#    Real-life 3D-2D metric (using image generator (DRR))
#
#===============================================================================
        print "Starting Registration 3..."
        # Set Fixed image using path
        fixedImage = JT.FixedImage()
        fixedImage.SetFileName(fixedImageName)

        reg3 = JT.Registration()
        reg3.SetFixedImage(fixedImage.GetImage(0))
        
        reg3.SetMovingImageGenerator(drr)
        
        roi = [[210,187],[277,370]]
        reg3.SetRegionOfInterest(roi)
        
        method = 'gc'
        reg3.SetImageMetric(method)
        pose = (0,0,0,0,0,0)
        val3 = reg3.GetValue(pose,debug=True,plot=True)
        
        #junk = raw_input()

#        print "Starting Registration 4..."
#        #calculate metric as 10deg Z rotation
#        pose = (0,0,0,0,0,10)
#        val4 = reg3.GetMetric(pose)
#        drr.InteractionOn()
#        drr.Render()
        val4 = 1

#===============================================================================
#
#    Print Results
#
#===============================================================================
        print "Registration1: %f, Registration2: %f,Registration3: %f,Registration4: %f" \
            %(val1,val2,val3,val4)
        print 'Method: ', method
        
        try:
            assert val1==0
            print "Null registration success!"
        except:
            print "Null registration failure"

        try:
            assert val2!=0
            print "Registration success"
        except:
            print "Registration failure"

        try:
            assert val3==val2
            print "DRR registration success"
        except:
            print "DRR registration failure"

        try:
            assert val4!=val3
            print "DRR pose registration success"
        except:
            print "DRR pose registration failure"


if __name__ == "__main__":
    test=Test()
    test.runTest()

